package com.cy.shiro.service.impl;

import com.cy.shiro.dao.SysResourcesDao;
import com.cy.shiro.pojo.SysResources;
import com.cy.shiro.service.SysResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysResourcesServiceImpl implements SysResourcesService {

    @Autowired
    private SysResourcesDao sysResourcesDao;

    @Override
    public SysResources findById(Integer id) {
        return sysResourcesDao.findById(id);
    }

    @Override
    public List<SysResources> resourcesList(Integer userId) {
        List<SysResources> sysResources = sysResourcesDao.resourcesList(userId);
        return sysResources;
    }

    @Override
    public List<SysResources> listAll() {
        return sysResourcesDao.listAll();
    }

    @Override
    public List<SysResources> listByUserId(Integer userId) {
        return sysResourcesDao.listByUserId(userId);
    }

    @Override
    public List<SysResources> listMenu() {
        return sysResourcesDao.listMenu();
    }

    @Override
    public List<SysResources> listButton(Integer parentId) {
        return sysResourcesDao.listButton(parentId);
    }

    @Override
    public List<SysResources> selectAllResoures(Map<String, String> map) {
        return sysResourcesDao.selectAllResoures(map);
    }

    @Override
    public List<SysResources> listAllAvailableMenu() {
        return sysResourcesDao.listAllAvailableMenu();
    }

    @Override
    public Integer addResources(SysResources sysResources) {
        return sysResourcesDao.addResources(sysResources);
    }

    @Override
    public Integer editResources(SysResources sysResources) {
        return sysResourcesDao.editResources(sysResources);
    }

    @Override
    public Integer delResources(Integer id) {
        return sysResourcesDao.delResources(id);
    }

    @Override
    public List<Map<String, Object>> queryResourcesListWithSelected(Integer id) {

        List<SysResources> sysResources = sysResourcesDao.queryResourcesListWithSelected(id);
        if(CollectionUtils.isEmpty(sysResources)){
            return null;
        }

        ArrayList<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map = null;
        for(SysResources resources : sysResources){
            map = new HashMap<String, Object>(3);
            map.put("id", resources.getId());
            map.put("pId", resources.getParentId());
            map.put("checked", resources.getChecked());
            map.put("name", resources.getName());
            mapList.add(map);
        }

        System.out.println(mapList);
        return  mapList;
    }


}
